package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.ruoyi.system.api.domain.SysRole;
import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.domain.XcxRoleMenu;
import com.ruoyi.system.mapper.XcxRoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.XcxRoleMapper;
import com.ruoyi.system.domain.XcxRole;
import com.ruoyi.system.service.IXcxRoleService;

/**
 * 小程序角色Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-29
 */
@Service
public class XcxRoleServiceImpl implements IXcxRoleService 
{
    @Autowired
    private XcxRoleMapper xcxRoleMapper;

    @Autowired
    private XcxRoleMenuMapper xcxRoleMenuMapper;
    /**
     * 查询小程序角色
     * 
     * @param xcxRoleId 小程序角色主键
     * @return 小程序角色
     */
    @Override
    public XcxRole selectXcxRoleByXcxRoleId(Long xcxRoleId)
    {
        XcxRole xcxRole= xcxRoleMapper.selectXcxRoleByXcxRoleId(xcxRoleId);
        Long[] data=xcxRoleMapper.selectXcxRoleByXcxRoleId_menu(xcxRoleId);
        xcxRole.setMenuIds(data);
        return xcxRole;
    }

    /**
     * 查询小程序角色列表
     * 
     * @param xcxRole 小程序角色
     * @return 小程序角色
     */
    @Override
    public List<XcxRole> selectXcxRoleList(XcxRole xcxRole)
    {
        return xcxRoleMapper.selectXcxRoleList(xcxRole);
    }


    /**
     * 新增角色菜单信息
     *
     * @param xcxRole 角色对象
     */
    public int insertRoleMenu(XcxRole xcxRole)
    {
        int rows = 1;
        // 新增用户与角色管理
        List<XcxRoleMenu> list = new ArrayList<XcxRoleMenu>();
        for (Long menuId : xcxRole.getMenuIds())
        {
            XcxRoleMenu rm = new XcxRoleMenu();
            rm.setXcxRoleId(xcxRole.getXcxRoleId());
            rm.setXcxMenuId(menuId);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            rows = xcxRoleMenuMapper.batchRoleMenu(list);
        }
        return rows;
    }
    /**
     * 新增小程序角色
     * 
     * @param xcxRole 小程序角色
     * @return 结果
     */
    @Override
    public int insertXcxRole(XcxRole xcxRole)
    {
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        xcxRole.setCreateData(dateFormat.format(calendar.getTime()));
        xcxRoleMapper.insertXcxRole(xcxRole);
        return insertRoleMenu(xcxRole);
    }

    /**
     * 修改小程序角色
     * 
     * @param xcxRole 小程序角色
     * @return 结果
     */
    @Override
    public int updateXcxRole(XcxRole xcxRole)
    {
        //删除权限
        xcxRoleMenuMapper.deleteXcxRoleMenuByXcxRoleId(xcxRole.getXcxRoleId());
        //补充权限
        insertRoleMenu(xcxRole);
        return xcxRoleMapper.updateXcxRole(xcxRole);
    }

    /**
     * 批量删除小程序角色
     * 
     * @param xcxRoleIds 需要删除的小程序角色主键
     * @return 结果
     */
    @Override
    public int deleteXcxRoleByXcxRoleIds(Long[] xcxRoleIds)
    {
        return xcxRoleMapper.deleteXcxRoleByXcxRoleIds(xcxRoleIds);
    }

    /**
     * 删除小程序角色信息
     * 
     * @param xcxRoleId 小程序角色主键
     * @return 结果
     */
    @Override
    public int deleteXcxRoleByXcxRoleId(Long xcxRoleId)
    {
        return xcxRoleMapper.deleteXcxRoleByXcxRoleId(xcxRoleId);
    }
}
